package hc.server;

import hc.core.ContextManager;
import hc.core.MsgBuilder;
import hc.core.data.DataClientAgent;
import hc.core.util.HCURL;
import hc.core.util.IHCURLAction;
import hc.core.util.LogManager;
import hc.server.data.KeyComperPanel;
import hc.server.data.screen.ScreenCapturer;
import hc.server.ui.ClientDesc;
import hc.server.ui.ServerUIUtil;

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class J2SEServerURLAction implements IHCURLAction {
	
	public boolean doBiz(HCURL url) {
		
		final String protocal = url.protocal;
		final String elementID = url.elementID;
		
//		hc.core.L.V=hc.core.L.O?false:LogManager.log("goto " + url.url);
		
		if(protocal == HCURL.SCREEN_PROTOCAL){
			if(elementID.equals(HCURL.REMOTE_HOME_SCREEN)){
				final byte[] homeBS = new byte[MsgBuilder.UDP_BYTE_SIZE];
				
				//送回本地的
				ScreenServer.cap = new ScreenCapturer(ClientDesc.clientWidth, ClientDesc.clientHight);
				DataClientAgent ss = ScreenServer.getScreenSize(homeBS);
				ContextManager.getContextInstance().send(MsgBuilder.E_SCREEN_REMOTE_SIZE, homeBS, ss.getLength());
				
				KeyComperPanel.sendExtMouse();
				
				ScreenServer.pushScreen(ScreenServer.cap);
				
				return true;
			}
		}else if(protocal == HCURL.CMD_PROTOCAL){
			if(elementID.equals(HCURL.DATA_CMD_EXIT)){
				if(ScreenServer.popScreen() == false){
//					System.out.println("Receiv Exit , and Notify exit by mobile");
					J2SEContext.notifyExitByMobi();
				}
				
				return true;
			}else if(elementID.equals(HCURL.DATA_CMD_SendPara)){
				String para1= url.getParaAtIdx(0);
				if(para1 != null && para1.equals(HCURL.DATA_PARA_RIGHT_CLICK)){
					String value1 = url.getValueofPara(para1);
					if(value1 != null){
						if(value1.equals(HCURL.DATA_PARA_VALUE_CTRL)){
							int x = Integer.parseInt(url.getValueofPara("x"));
							int y = Integer.parseInt(url.getValueofPara("y"));

							hc.core.L.V=hc.core.L.O?false:LogManager.log(ScreenCapturer.OP_STR + "Ctrl + mouse left click at (" + x + ", " + y + ")");
							
							doClickAt(url, KeyEvent.VK_CONTROL, x, y);
							return true;
						}else if(value1.equals(HCURL.DATA_PARA_VALUE_SHIFT)){
							int x = Integer.parseInt(url.getValueofPara("x"));
							int y = Integer.parseInt(url.getValueofPara("y"));

							hc.core.L.V=hc.core.L.O?false:LogManager.log(ScreenCapturer.OP_STR + "Shift + mouse left click at (" + x + ", " + y + ")");
							
							doClickAt(url, KeyEvent.VK_SHIFT, x, y);
							return true;
						}
					}
				}else if(para1 != null && para1.equals(HCURL.DATA_PARA_CERT_RECEIVED)){
					J2SEContext.isTransedToMobileSize = true;
					return true;
				}
			}
		}
		
		return ServerUIUtil.responsor.doBiz(url);
	}

	private void doClickAt(HCURL url, int mode, int x, int y) {
		ScreenCapturer.robot.mouseMove(x, y);
		
		ScreenCapturer.robot.keyPress(mode);

		ScreenCapturer.robot.mousePress(InputEvent.BUTTON1_MASK);
		ScreenCapturer.robot.mouseRelease(InputEvent.BUTTON1_MASK);

		ScreenCapturer.robot.keyRelease(mode);
	}

}
